﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.GData.Extensions.Location;
using Google.GData.Client;
using System.IO;

public partial class uploadViDeo : System.Web.UI.Page
{
    VLGDataContext _db8 = new VLGDataContext();
    CommonPage cm45 = new CommonPage();
    protected void Page_Load(object sender, EventArgs e)
    {
        string idGig= Request.QueryString["gi"];
        if (!IsPostBack)
        {
            if (Session["KhachHang"] == null)
            {
                    Response.Redirect(ResolveClientUrl("accessDenied.aspx"));
            }
            else if (String.IsNullOrEmpty(idGig))
            {

                    Response.Redirect(ResolveClientUrl("accessDenied.aspx"));
                // loadDataDrop(idAcc);
            }
            else
            {
                try
                {
                    int idCus = cm45.Customer.idAcc;
                    Gig gid = _db8.Gigs.Single(aco => aco.idGig == int.Parse(idGig));
                    if (gid.idAcc != idCus)
                    {
                        
                            Response.Redirect(ResolveClientUrl("accessDenied.aspx"));
                    }
                    if (gid.LinkVideo != null)
                    {
                        Response.Redirect(ResolveClientUrl("yourPage.aspx"));
                    }
                    else
                    {
                    }
                }
                catch
                {
                    Response.Redirect(ResolveClientUrl("accessDenied.aspx"));
                }
            }
        }
    }
    //void loadDataDrop(int idUser)
    //{
    //    var itemGig = from gig9 in _db8.Gigs
    //                  where gig9.AnHien == true && gig9.idAcc == idUser && gig9.LinkVideo==null
    //                  select gig9;
    //    dropGig.DataSource = itemGig;
    //    dropGig.DataTextField="TenCongViec";
    //    dropGig.DataValueField="idGig";
    //    dropGig.DataBind();
    //}
  
   
    //protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    //{
    //    System.Threading.Thread.Sleep(5000);
    //    if (AsyncFileUpload1.HasFile)
    //    {
    //        string strPath = MapPath("videos/") + Path.GetFileName(e.FileName);
    //        AsyncFileUpload1.SaveAs(strPath);
    //    }

    //}
    protected void Button1_Click(object sender, EventArgs e)
    {
        string igicode= Request.QueryString["gi"];
        if (FileUpload1.HasFile)
        {
            if (FileUpload1.PostedFile.ContentLength > 4194304)
            {
                Label1.Text = "Kích thước ảnh quá lớn. Kích thước ảnh phải bé hơn 4 MB";
                return;
            }
            else
            {
                string[] name=Path.GetFileName(FileUpload1.FileName).Split('.');
                string ten=name[0];
                string duoi=name[1];
                string tenmoi=ten+DateTime.Now.Ticks.ToString()+"."+duoi;
                try
                {
                    FileUpload1.SaveAs(Server.MapPath("videos/") + tenmoi);
                    Gig gigid = _db8.Gigs.Single(all=>all.idGig==int.Parse(igicode));

                    gigid.LinkVideo = tenmoi;
                    _db8.SubmitChanges();
                    Response.Redirect(ResolveClientUrl("~/yourPage.aspx"));
                }
                catch
                {
                    Label1.Text = "Có lỗi trong quá trình upload. Vui lòng thử lại sau";
                    return;
                }
            }
        }
    }
}
